// 验证用户是否登录状态, 不是则提示登录
$.ajax({
    type: 'post',
    url: 'user',
    dataType: 'text',
    success: function (body) {
        if (body == 'null'){
            alert("未登录, 请先登录");
            location = 'login.html'
        }
    }
})

let getUserMessage = function(user){
    let photo = document.querySelector("#photo");
    let username = document.querySelector("#username");
    let github = document.querySelector("#github");
    let fans = document.querySelector("#fans");
    username.innerHTML = user.username;
    photo.src = './image/' + user.photo + '.png';
    if (fans){
        fans.innerHTML = user.fans.length;
    }
    if (github){
        if (user.github && user.github != '' && user.github != 'null' && user.github != 'NULL'){
            github.href = user.github;
        }else{
            github.innerHTML = '';
        }
    }
}

let getUserBlogList = function (userId) {
    // 获取 count的文章
    let article = document.querySelector("#article");
    let count = document.querySelector("#count");
    let articles;
    $.ajax({
        type: 'get',
        url: 'blog' + "?userId=" + userId,
        success: function (body) {
            count.innerHTML = body.length;
            articles = body
        }
    })
    article.onclick = function () {
        let right = document.querySelector(".right");
        right.innerHTML = '';// 清空原有的列表
        for (let every of articles){
            let blog = document.createElement('div');
            blog.className = 'blog';

            let title = document.createElement('div');
            title.innerHTML = every.title;
            title.className = 'title';
            let date = document.createElement('div');
            date.innerHTML = every.postTime;
            date.className = 'date';
            let desc = document.createElement('div');
            desc.innerHTML = every.desc;
            desc.className = 'desc';
            let a = document.createElement("a");
            a.innerHTML =  '查看全文 &gt;&gt;';
            a.href = 'blogDetail.html?blogId=' + every.blogId + "&authorId=" + every.userId;
            // 挂载到上面
            blog.appendChild(title);
            blog.appendChild(date);
            blog.appendChild(desc);
            blog.appendChild(a);

            right.appendChild(blog);
        }
    }
}

let getFansList = function (fans) {
    // 获取 粉丝数
    let fansList = document.querySelector("#fansList");
    fansList.onclick = function () {
        let right = document.querySelector(".right");
        right.innerHTML = '';// 清空原有的列表
        for (let every of fans){
            let fan = document.createElement('a');
            fan.href = 'chat.html'+'?authorId='+every.userId;
            fan.className = 'fan';

            let img = document.createElement("img");
            img.src = './image/' + every.photo + '.png';
            img.className = 'img'

            let username = document.createElement('span');
            username.innerHTML = every.username;

            fan.appendChild(img);
            fan.appendChild(username);

            right.appendChild(fan);
        }
    }
}